package com.xdtech.trans.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.common.utils.EmptyUtil;
import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.trans.dao.PriceConfigDao;
import com.xdtech.trans.model.PriceConfig;
import com.xdtech.trans.service.PriceConfigService;
import com.xdtech.trans.vo.PriceConfigItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-07-04 21:24:57
 * @since 1.0
 * @see
 */
@Service
public class PriceConfigServiceImpl implements PriceConfigService {
	private Log log = LogFactory.getLog(PriceConfigServiceImpl.class);
	@Autowired
	private PriceConfigDao priceConfigDao;
	@Autowired
	private BaseDao baseDao;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-07-04 21:24:57
	 * @modified by
	 * @param entity
	 */
	public void save(PriceConfig entity) {
		priceConfigDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-07-04 21:24:57
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<PriceConfig> entities) {
		priceConfigDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-07-04 21:24:57
	 * @modified by
	 * @param entity
	 */
	public void delete(PriceConfig entity) {
		priceConfigDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-07-04 21:24:57
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		priceConfigDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-07-04 21:24:57
	 * @modified by
	 * @param id
	 * @return
	 */
	public PriceConfig get(Long id) {
		return priceConfigDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-07-04 21:24:57
	 * @modified by
	 * @return
	 */
	public List<PriceConfig> getAll() {
		return priceConfigDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-07-04 21:24:57
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> priceConfigs = null;
		long rows = 0;
		if (pg!=null) {
			Page<PriceConfig> page = priceConfigDao.findPage(pg,"from PriceConfig order by createTime desc", values);
			priceConfigs = BeanUtils.copyListProperties(
					PriceConfigItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<PriceConfig> priceConfigList = priceConfigDao.find("from PriceConfig order by id desc", values);
			priceConfigs = BeanUtils.copyListProperties(
					PriceConfigItem.class, priceConfigList);
			rows = priceConfigs.size();
		}
		results.put("total", rows);
		results.put("rows", priceConfigs);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-07-04 21:24:57
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdatePriceConfig(PriceConfigItem item) {
		PriceConfig priceConfig = null;
		if (item.getId()==null) {
			priceConfig = new PriceConfig();
		}else {
			priceConfig = priceConfigDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(priceConfig, item);
		priceConfigDao.save(priceConfig);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-07-04 21:24:57
	 * @modified by
	 * @param priceConfigId
	 * @return
	 */
	public PriceConfigItem loadPriceConfigItem(Long priceConfigId) {
		PriceConfig priceConfig = priceConfigDao.get(priceConfigId);
		return PriceConfigItem.createItem(priceConfig);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-07-04 21:24:57
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deletePriceConfigInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-07-04 21:24:57
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deletePriceConfigInfo(List<Long> priceConfigIds) {
		for (Long id : priceConfigIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-07-04 21:24:57
	 * @modified by
	 * @return
	 */
	@Override
	public List<PriceConfigItem> loadItems() {
		List<PriceConfig> priceConfigs = getAll();
		return PriceConfigItem.createItems(priceConfigs);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-7-4下午9:59:25
	 * @modified by
	 * @return
	 */
	@Override
	public PriceConfigItem loadFirstPriceConfigItem() {
		List<PriceConfigItem> priceConfigItems = loadItems();
		return EmptyUtil.isNotEmpty(priceConfigItems)?priceConfigItems.get(0):null;
	}

}
